package com.ask.forMe.service.impl;

import com.ask.forMe.model.dto.GroupByDTO;
import com.ask.forMe.model.dto.OrderInfoDTO;
import com.ask.forMe.model.entity.GroupBuyActivity;
import com.ask.forMe.model.entity.GroupBuyMember;
import com.ask.forMe.model.entity.GroupBuyTeam;
import com.ask.forMe.model.entity.Order;
import com.ask.forMe.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class GroupBuyServiceImpl implements GroupBuyService {
    @Autowired
    private GroupBuyTeamService groupBuyTeamService;
    @Autowired
    private GroupBuyActivityService groupBuyActivityService;
    @Autowired
    private GroupBuyMemberService groupBuyMemberService;
    @Autowired
    private OrderService orderService;

    /**
     * 用户发起拼单
     *
     * @param groupByDTO
     */
    @Override
    public void createTeam(GroupByDTO groupByDTO) {
        GroupBuyTeam team = GroupBuyTeam.builder()
                .activityId(groupByDTO.getActivityId())
                .createTime(LocalDateTime.now())
                .currentMembers(1)
                .leaderId(groupByDTO.getUserId())
                .expireTime(LocalDateTime.now().plusDays(2))
                .status(GroupBuyTeam.STATUS_ACTIVE).build();
        groupBuyTeamService.save(team);

        // 订单创建
        OrderInfoDTO orderInfo = groupByDTO.getOrderInfoDTO();
        createOrder(orderInfo);;
    }

    /**
     * 用户加入拼团
     *
     * @param groupByDTO
     * @return
     */
    @Override
    public boolean joinTeam(GroupByDTO groupByDTO) {
        // 解析团购信息
        Long activityId = groupByDTO.getActivityId();
        Long teamId = groupByDTO.getTeamId();
        GroupBuyActivity activity = groupBuyActivityService.getById(activityId);
        GroupBuyTeam team = groupBuyTeamService.getById(teamId);

        if (team.getStatus() != GroupBuyTeam.STATUS_ACTIVE || team.getCurrentMembers() >= activity.getRequiredMembers())
            return false;

        // 跟新拼团人数
        team.setCurrentMembers(team.getCurrentMembers() + 1);
        groupBuyTeamService.save(team);

        // 订单创建
        OrderInfoDTO orderInfo = groupByDTO.getOrderInfoDTO();
        createOrder(orderInfo);

        // 添加成员
        saveMember(groupByDTO, orderInfo);

        // 检查是否拼成团
        if (team.getCurrentMembers() >= activity.getRequiredMembers())
            groupBuyTeamService.completeTeam(teamId);
        return true;
    }

    /**
     * 添加拼团成员
     * @param groupByDTO
     * @param orderInfo
     */
    private void saveMember(GroupByDTO groupByDTO, OrderInfoDTO orderInfo) {
        GroupBuyMember groupBuyMember =
                GroupBuyMember
                        .builder().
                        teamId(groupByDTO.getTeamId())
                        .userId(groupByDTO.getUserId())
                        .payStatus(GroupBuyMember.STATUS_PENDING)
                        .joinTime(LocalDateTime.now())
                        .orderNo(orderInfo.getOrderNo())
                        .orderId(orderInfo.getOrderId())
                        .build();
        groupBuyMemberService.save(groupBuyMember);
    }

    /**
     * 创建订单
     * @param orderInfo
     */
    private void createOrder(OrderInfoDTO orderInfo) {
        // 生成订单号
        String orderNo = orderService.createOrderNo();
        Order order = Order.builder().
                userId(orderInfo.getUserId())
                .itemId(orderInfo.getItemId())
                .quantity(orderInfo.getQuantity())
                .createTime(LocalDateTime.now())
                .status(Order.STATUS_PAYMENT)
                .orderNo(orderNo)
                .build();
        orderService.save(order);
        orderInfo.setOrderId(order.getId());
        orderInfo.setOrderNo(orderNo);
    }
}
